//
//  UIViewController+ConfigUI.swift
//  CarHailingDriver
//
//  Created by xiqiang.zhu on 2019/7/4.
//  Copyright © 2019 iOS-team. All rights reserved.
//

import Foundation
import DynamicColor

private let restorationIdentifier = "UIViewControllerAbnormalViewRestorationIdentifier "

extension UIViewController {
    public func initValue() {
        view.backgroundColor = DynamicColor(hex: 0xf3f3f3)
        navigationController?.navigationBar.barTintColor = .white
        navigationController?.navigationBar.isTranslucent = false
        let count = self.navigationController?.viewControllers.count
        if count ?? 0 > 1 {
            let image = UIImage.init(named: "icon_nav_arrow_back")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            let backItem : UIBarButtonItem = UIBarButtonItem.init(image:image , style:.plain, target: self, action: #selector(back(sender:)))
            navigationItem.leftBarButtonItem = backItem
        }
        navigationController?.navigationBar.shadowImage = UIImage.init()
    }
    
    /// 显示导航栏左侧返回按钮
    func showLeftNavigationItemBackButton() -> () {
        showNavigationLeftButton(title: "", imgName: "icon_nav_arrow_back")
    }
    /// 显示导航栏左侧菜单按钮
    func showLeftNavigationItemMenuButton() -> () {
        showNavigationLeftButton(title: "", imgName: "icon_nav_menu")
    }
    
    /// 显示导航栏左侧按钮
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - imgName: 图片名称
    fileprivate func showNavigationLeftButton(title:String?,imgName:String?) -> () {
        let leftBar:UIBarButtonItem
        
        if let imageName = imgName {
            let image = UIImage.init(named: imageName)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            leftBar = UIBarButtonItem.init(image: image, style: .plain, target: self, action: #selector(self.leftBarAction))
            navigationItem.leftBarButtonItem = leftBar

        }else {
            if let leftTitle = title {
             leftBar = UIBarButtonItem.init(title: leftTitle, style: .plain, target: self, action: #selector(self.leftBarAction))
                navigationItem.leftBarButtonItem = leftBar
            }
            
        }
    }
    
    /// 显示导航栏右侧按钮
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - imgName: 图片名称
    func showNavigationRightButton(title:String?,imgName:String?) -> () {
        let rightBar:UIBarButtonItem
        
        if let imageName = imgName {
            let image = UIImage.init(named: imageName)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            rightBar = UIBarButtonItem.init(image: image, style: .plain, target: self, action: #selector(self.rightBarAction))
            navigationItem.rightBarButtonItem = rightBar
            
        }else if let  rightTitle = title{
            
            let rightButton = UIButton(type: UIButton.ButtonType.custom)
            rightButton.setTitle(rightTitle, for: UIControl.State.normal)
            rightButton.setTitleColor(.white, for: UIControl.State.normal)
            
            rightButton.addTarget(self, action: #selector(self.rightBarAction), for: .touchUpInside)
            navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton)
            
        }
    }
    
    // MARK: - public method
    @objc public func back(sender:Any) {
        navigationController!.popViewController(animated: true)
    }
    
    @objc  func leftBarAction() -> () {
        
    }
    
    @objc  func rightBarAction() -> () {
        
    }
    
    
    
}
